
package com.liwt.offer.chapter2;

/**
 * 单例模式下考虑多线程并发的情况
 * 
 * @author likebamboo
 */
public class Singleton {

    // 加上同步锁
    private static Object lock = new Object();

    private static Singleton mInstance = null;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (mInstance == null) {
            synchronized (lock) {
                if (mInstance == null) {
                    mInstance = new Singleton();
                }
            }
        }
        return mInstance;
    }
}
